package br.com.consultsystems.interfaces.wrapper;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

public class DateTypeAdapter extends TypeAdapter<Date> {
	
	@Override
	public Date read(JsonReader reader) throws IOException {
		
		/*
		 * Original
		 * return new BigDecimal(reader.nextString());
		 */
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		try {
			return sdf.parse(reader.nextString());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return new Date();
	}

	@Override
	public void write(JsonWriter writer, Date value) throws IOException {

		/*
		 * Original
		 * writer.value(value);
		 */
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		writer.value(sdf.format(value));
	}
}
